package com.anysoft.view;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

import com.anysoft.R;
import com.anysoft.utils.DisplayUtil;

/**
 * @ProjectName：gy_fund-android
 * @ClassDescription：
 * @CreateUser： yanbin
 * @CreateTime：2017-06-14 10:06
 * @ModifiedUser：
 * @ModifiedTime：
 * @ModifiedRemark：
 */

public class HProgressBarLoading extends View{

    private Context mContext;
    private int mMax;//进度条最大的进度
    private int mDefaultHeight;//高度
    private int mCurProgress;//当前的进度
    private int mWidth;
    private int mHeight;
    private Paint mPaint;
    private int mColor;

    public HProgressBarLoading(Context context) {
        this(context, null);
    }


    public HProgressBarLoading(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }


    public HProgressBarLoading(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initAttr(attrs);
    }


    private void initAttr(AttributeSet attrs) {
        TypedArray array = mContext.obtainStyledAttributes(attrs, R.styleable.hprogress);
        mMax = array.getInt(R.styleable.hprogress_max, 10);
        mCurProgress = array.getInt(R.styleable.hprogress_progress, 0);
        mDefaultHeight = array.getInt(R.styleable.hprogress_progressHeight, 200);
        mColor = array.getColor(R.styleable.hprogress_progressColor, Color.parseColor("#E20404"));
        array.recycle();

        mPaint = new Paint();
        mPaint.setColor(mColor);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthSize = View.MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = View.MeasureSpec.getSize(heightMeasureSpec);
        int widthMode = View.MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = View.MeasureSpec.getMode(heightMeasureSpec);
        //矩形宽度为view的80%
        if (widthMode == View.MeasureSpec.EXACTLY) {
            mWidth = widthSize;
        } else if (widthMode == View.MeasureSpec.AT_MOST) {
            //mDefaultWidth 为你自定义设置的属性
            mWidth = DisplayUtil.dp2px(mContext, 300);
        } else if (widthMode == View.MeasureSpec.UNSPECIFIED) {
            mWidth = widthSize;
        }


        if (heightMode == View.MeasureSpec.EXACTLY) {
            mHeight = heightSize;
        } else if (heightMode == View.MeasureSpec.AT_MOST) {
            //mDefaultHeight 为你自定义设置的属性
            mHeight = DisplayUtil.dp2px(mContext, mDefaultHeight);
        }
        setMeasuredDimension(mWidth, mHeight);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);


        float result = mWidth * ((float) mCurProgress / (float) 100);
        canvas.drawRect(0, 0, result, mDefaultHeight, mPaint);
    }


    public int getMax() {
        return mMax;
    }


    public void setMax(int max) {
        mMax = max;
    }


    public int getProgressBarHeight() {
        return mDefaultHeight;
    }

    public void setProgressBarHeight(int mDefaultHeight) {
        this.mDefaultHeight = mDefaultHeight;
    }

    public int getProgressBarColor() {
        return mColor;
    }

    public void setProgressBarColor(int mColor) {
        this.mColor = mColor;
    }

    public int getCurProgress() {
        return mCurProgress;
    }


    boolean isLoading = false;
    boolean isEndLoading = false;
    int time = 0;
    public void setCurProgress(int curProgress) {
        if(curProgress==100){
            isEndLoading = true;
        }
        setCurProgress(curProgress,false);
    }
    private void setCurProgress(int curProgress, final boolean isLoadEnd) {
//        Log.e("aaaaa",System.currentTimeMillis()+"*"+curProgress);

        setVisibility(VISIBLE);
        if (mCurProgress == 100) {//重置mCurProgress为0
            mCurProgress = 0;
        }
        if(isLoading){
            return;
        }
        isLoading = true;
        if(curProgress==100) {
            time  =500;
        }else{
            time = 800;
            curProgress = 80;
        }
//        Log.e("aaaaa","*****"+curProgress);
        //注意是从 mCurProgress->curProgress 来动画来实现
        ValueAnimator animator = ValueAnimator.ofInt(mCurProgress, curProgress);
        animator.setDuration(time);
        animator.setInterpolator(new LinearInterpolator());
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mCurProgress = (int) animation.getAnimatedValue();
                postInvalidate();//通知刷新
            }
        });
        animator.start();
        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationEnd(Animator animation) {
                isLoading = false;
                if(isEndLoading && mCurProgress!=100 && !isLoadEnd){
                    setCurProgress(100,true);
                }
                if(mCurProgress==100){
                    isEndLoading = false;
                    setVisibility(GONE);
                }
            }
            @Override
            public void onAnimationStart(Animator animation) {
            }
            @Override
            public void onAnimationCancel(Animator animation) {
            }
            @Override
            public void onAnimationRepeat(Animator animation) {
            }
        });
    }
}
